package com.example.planetexploder.module.bookshelf;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.planetexploder.adapter.MyAdapter_BookInfo;
import com.example.planetexploder.R;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.module.book.View_Book;
import com.example.planetexploder.module.firstpage.View_FirstPage;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

/**
 * 书架页面的显示页面，实现显示、长按删除、和新建功能
 * @author lxx
 * 2019.11.1
 */

public class View_Content extends Fragment implements AdapterView.OnItemLongClickListener{

    private Presenter_BookShelf presenterBookShelf;
    private GridView mGridView;
    private MyAdapter_BookInfo mAdapter;
    private boolean isShowDelete = false;
    private View root;
    private ArrayList<BookInfo> mList;

    /**
     * 功能：
     * 2019.10.19
     */
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.activity_firstpage, container, false);
        presenterBookShelf = new Presenter_BookShelf(getActivity());
        /**
         * 功能：底部的跳转功能实现
         */
        ImageView imageView_book = root.findViewById(R.id.book_unchosed);
        imageView_book.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), View_FirstPage.class);
                startActivity(intent);
            }
        });

        /**
         * 功能：监控加(+)号按钮的单击事件行为并作出处理
         */
        FloatingActionButton buttonPlusBookShelf = root.findViewById(R.id.buttonPlusFirst);
        buttonPlusBookShelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                plusButtonBookshelf(getActivity());
            }
        });

        /**
         * 功能：初始化显示页面信息并进行绑定
         */
        mList = presenterBookShelf.get_ShelfTable(presenterBookShelf.get_Tag1(), presenterBookShelf.get_Tag2());

        mGridView = root.findViewById(R.id.gridViewFirst);

        mGridView.setOnItemLongClickListener(View_Content.this);
        mAdapter = new MyAdapter_BookInfo(getActivity(), mList);
        mGridView.setAdapter(mAdapter);
        /**
         * 功能：设置GridView中item为单击事件，并根据单击效果进入编辑模式
         */
        GridView gridView = root.findViewById(R.id.gridViewFirst);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                transmitBookInfo(mList.get(position));
            }
        });

        return root;
    }

    /**
     * 功能：实现对显示页面中的长按删除功能
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isShowDelete) {
            isShowDelete = false;
        } else {
            isShowDelete = true;
            mAdapter.setIsShowDelete(isShowDelete);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                        presenterBookShelf.delete_Book(mList.get(position).bookId);
                        deleteGridViewItems(position);//删除选中项
                        refreshGridView();
                }
            });
        }
        mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
        return true;
    }

    /**
     * 功能：删除用户长按删除时的选中项
     */
    private void deleteGridViewItems(int position) {
        ArrayList<BookInfo> newList = new ArrayList<BookInfo>();
        if (isShowDelete) {
            mList.remove(position);
            isShowDelete = false;
        }
        newList.addAll(mList);
        mList.clear();
        mList.addAll(newList);

        GridView gridView = root.findViewById(R.id.gridViewFirst);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                transmitBookInfo(mList.get(position));
            }
        });
    }

    /**
     * 功能：刷新ArrayList数组并重新绑定容器
     */
    public void refreshGridView() {
        mList = presenterBookShelf.get_ShelfTable(presenterBookShelf.get_Tag1(),presenterBookShelf.get_Tag2());
        mAdapter = new MyAdapter_BookInfo(getActivity(), mList);
        mGridView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    /**
     *功能：已BookInfo为参数进行页面跳转
     */
    public void transmitBookInfo(BookInfo bookInfo) {
        Intent intent = new Intent(getActivity(),View_Book.class);
        intent.putExtra("BookInfo",bookInfo);
        startActivity(intent);
    }

    /**
     * 功能：显示创建摘抄集的对话框
     */
    public void plusButtonBookshelf(final Context context){
        final EditText textBookShelf = new EditText(context);
        new AlertDialog.Builder(context).setTitle("新建摘抄集")
                .setView(textBookShelf).setNegativeButton("取消", null)
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
            //保存摘抄集名字
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if(textBookShelf.getText().toString().length()> 0)
                {
                    if(textBookShelf.getText().toString().length() <30) {
                        invariantActivity(textBookShelf.getText().toString());
                    }
                    else {
                        Toast.makeText(getActivity(), "您输入的文字字数已到上限", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(getActivity(), "您输入的内容不能为空", Toast.LENGTH_SHORT).show(); }
            }
        }).show();
    }

    /**
     * 功能：创建摘抄集后返回原Activity
     */
    public void invariantActivity(String text){
        presenterBookShelf.create_Book(text);
        Intent intentNow = new Intent(getActivity(),View_Fragment.class);
        startActivity(intentNow);
    }
}